<?php
/**
 * 功能描述：	用户登录注册模块
 * @datetime	2022-07-08
 * @version   	v2.3.0
 * @author   	ZHL
 * @copyrigh  	2022 ZHL 版权所有
 */

class Admin_LoginServe extends Model {

    /**
     * Notes:验证用户信息
     * User: ZhuHaili
     * Date: 2023/2/24
     */
    public function checkUser($manageUser, $managePwd, $manageCode, $manageKey){
        if(!$manageUser){ return '开发者账号不能为空'; }
        if(!$managePwd){ return '开发者密码不能为空'; }
        if(!$manageKey){ return '开发者密钥不能为空'; }
        if($manageUser != SYS_DEVELOPER){return '开发者账号错误';}
        if($manageKey !== SYS_DEV_LOGIN_FLAG){ return '开发者密钥错误';}
        $verifyFlag = isset($GLOBALS['ADMIN']['verify_flag']) ? $GLOBALS['ADMIN']['verify_flag'] : 0;
        if($verifyFlag == '1') {
            if(!$manageCode){ return '图形验证密码不能为空'; }
            $imgCodeArr = SYS_SESSIONS_GET('a_img_code');
            $imgTime = isset($imgCodeArr['times']) ? $imgCodeArr['times'] : 0;
            $imgCode = isset($imgCodeArr['imgcode']) ? $imgCodeArr['imgcode'] : 0;
            if($imgCode != $manageCode){
                return '图形验证码不正确';
            }else if ((time() - $imgTime) > 300){ // 有效5分钟
                return '图形验证码失效请刷新重试';
            }
        }
        $pwd = setPwd($managePwd);
        $pwd2 = setPwd($manageUser . date('Ymd'));
        if ($pwd != $pwd2) { return '开发者密码错误'; }
        return 'Y';
    }

    /**
     * Notes: 用户登录操作
     * User: ZhuHaili
     * Date: 2022/7/8
     * @return string
     */
	function goUserLogin($manageUser, $managePwd, $manageCode, $manageKey){
        if(isset($GLOBALS['ADMIN']['admin_uphold']) && intval($GLOBALS['ADMIN']['admin_uphold']) == 1){
            if($manageUser != SYS_DEVELOPER){
                return '后台维护中……';
            }
        }
		if(!$manageUser){ return '账号不能为空'; }
		if(!$managePwd){ return '密码不能为空'; }
        $verifyFlag = isset($GLOBALS['ADMIN']['verify_flag']) ? $GLOBALS['ADMIN']['verify_flag'] : 0;
        // 开启图片验证
        if($verifyFlag == '1') {
            if(!$manageCode){ return '验证密码不能为空'; }
            $imgCodeArr = SYS_SESSIONS_GET('a_img_code');
            $imgTime = isset($imgCodeArr['times']) ? $imgCodeArr['times'] : 0;
            $imgCode = isset($imgCodeArr['imgcode']) ? $imgCodeArr['imgcode'] : 0;
            if($imgCode != $manageCode){
                return '验证码不正确';
            }else if ((time() - $imgTime) > 300){ // 有效5分钟
                return '验证码失效请刷新重试';
            }
        }
        $pwd = setPwd($managePwd);
        // 开发者
        if($manageUser == SYS_DEVELOPER){
            $pwd2 = setPwd($manageUser . date('Ymd'));
            if ($manageKey !== SYS_DEV_LOGIN_FLAG){ return '没有获得登录权限';}
            if ($pwd != $pwd2) { return '账号或密码错误'; }
            $find['id'] = 1;
            $find['nickname'] = $manageUser;
            $find['levels'] = 1;
            $find['status'] = '1';
            $find['headimg'] = ADMIN_HEAD_PIC;
            $find['logincount'] = '1';
        }else{
            $login = new Admin_LoginModel();
            $userFlag = isset($GLOBALS['ADMIN']['user_flag']) ? $GLOBALS['ADMIN']['user_flag'] : '1';
            if ($userFlag == '1') {
                $find = $login->getUserInfoOther($manageUser, $pwd); // 昵称、手机、邮箱即可作为登录账号
            } else {
                $find = $login->getUserInfoUser($manageUser, $pwd); // 只有昵称作为登录账号
            }
            if (empty($find)) { return '账号或密码错误'; }
            if (!$find['headimg']){ $find['headimg'] = ADMIN_HEAD_PIC; }
            if($find['status'] == 2){return '账号已禁用';}
            if($find['status'] == 3){return '账号已注销';}
            /** 登录日志 **/
            $logLoginSer = new Admin_SyslogServe();
            $logLoginSer->addLoginLog($find);
        }
        $find['logintime'] = date('Y-m-d H:i:s');
        $find['website'] = md5(SYS_HOST);
        SYS_SESSIONS_SET(LOGIN_SESSION_FIX.'alogin_info', $find);
        return 'Y';
	}

    /**
     * Notes: 判断是否登录
     * User: ZhuHaili
     * Date: 2022/7/8
     */
    function judgeSessionLogin($controller, $action, $type = 0, $content = ''){
        $loginInfo = SYS_SESSIONS_GET(LOGIN_SESSION_FIX.'alogin_info');
        $loginInfoId = isset($loginInfo['id']) ? $loginInfo['id'] : '';
        $loginInfoWeb = isset($loginInfo['website']) ? $loginInfo['website'] : '';
        $webHttpUrl = md5(SYS_HOST);
        if(!$loginInfoId || ($loginInfoWeb != $webHttpUrl)){
            if($type){
                SYS_REDIRECT('admin_login');
            }else{
                htmlAlert('超时！请重新登录', SYS_URL_PATH('admin_login'));
            }
        }
        if(isset($GLOBALS['ADMIN']['admin_uphold']) && intval($GLOBALS['ADMIN']['admin_uphold']) == 1){
            if($loginInfo['levels'] != 1){
                htmlAlert('后台维护中……', SYS_URL_PATH('admin_login'));
            }
        }

        $menu_id = Fun::input('c', 0, 'd');
        $menu_id = $menu_id ? $menu_id : Fun::input('menu_id', 0, 'd');
        $controller = strtolower($controller);
        $action = strtolower($action);
        if($loginInfo['levels'] != 1){
            // 判断用户权限
            $powerSer = new Admin_PowerServe();
            $powerSer->judgeUserPower($controller, $action, $menu_id);
            // 添加操作日志
            $logSer = new Admin_SyslogServe();
            $logSer->addOperateLog($controller, $action, $menu_id, $content = '');
        }
    }

    /**
     * Notes:根据Id获取用户基本信息
     * User: ZhuHaili
     * Date: 2022/7/8
     */
    function getUserInfoNo($id){
        if(!$id){
            return array();
        }
        $login = new Admin_LoginModel();
        return $login->getUserInfoNo($id);
    }

}